package com.dome.backtemplate.util;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.jwt.JWT;
import cn.hutool.jwt.JWTPayload;
import cn.hutool.jwt.JWTUtil;
import com.dome.backtemplate.constant.SystemConstant;


import java.util.HashMap;
import java.util.Map;

/**
 * @Description TODO
 * @Author 小乌龟
 * @Date 2023/4/5 15:22
 */
public class JwtUtil {
    public static String createToken(String pkId){
        Map<String,Object> payload = new HashMap<String,Object>();
        DateTime now = DateTime.now();
        DateTime expiresTime = now.offsetNew(DateField.SECOND, SystemConstant.NUM_24_time);
        payload.put(SystemConstant.JWT_USER_INFO,pkId);
        //生效时间
        payload.put(JWTPayload.NOT_BEFORE, now);
        //过期时间
        payload.put(JWTPayload.EXPIRES_AT, expiresTime);
        return JWTUtil.createToken(payload, SystemConstant.JWT_KEY.getBytes());
    }

    public static String getTokenInfo(String token) {
        JWT jwt = JWTUtil.parseToken(token);
        return  (String) jwt.getPayload(SystemConstant.JWT_USER_INFO);
    }
}
